package com.hst.web.base.proxy;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.hst.core.Page;
import com.hst.core.ServiceContext;
import com.hst.core.dao.IORDao;
import com.hst.core.dao.ISQLDao;
import com.hst.core.dao.ORQuery;
import com.hst.core.dao.ORQuery.Op;
import com.hst.core.meta.MetaData;
import com.hst.core.meta.proxies.DefaultDataProxy;
import com.hst.core.meta.proxies.OREntityUtils;
import com.hst.web.auth.po.User;
import com.hst.web.base.entity.TCompanyContractor;
import com.hst.web.base.entity.TContractorInfo;
import com.hst.web.base.service.UserRoleService;
import com.hst.web.base.ui.ContractorInfo;
import com.hst.web.sys.entity.TRole;

public class ContractorProxy extends DefaultDataProxy {

	@Autowired
	UserRoleService us;

	@Autowired
	IORDao dao;

	@Autowired
	ISQLDao sql;

	@Autowired
	OREntityUtils orUtil;

	@SuppressWarnings("unchecked")
	@Override
	public Page<?> list(Map<String, ?> query, int pageIdx) {
		User u = (User) ServiceContext.getInstance().getUser();
		TRole role = us.userRole(u.getId());
		if ("9".equals(role.getRoleLevel())) {
			return sql.list("contractor.listContractors", query, pageIdx, Page.getPageSize());
		} else {
			Map<String, Object> map = (Map<String, Object>) query;
			map.put("companyCode", u.getCompanyCode());
			return sql.list("contractor.listContractors", map, pageIdx, Page.getPageSize());
		}
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<?> list(Map<String, ?> query) {
		User u = (User) ServiceContext.getInstance().getUser();
		TRole role = us.userRole(u.getId());
		if ("9".equals(role.getRoleLevel())) {
			return sql.query("contractor.listContractors", query);
		} else {
			Map<String, Object> map = (Map<String, Object>) query;
			map.put("companyCode", u.getCompanyCode());
			return sql.query("contractor.listContractors", map);
		}
	}

	@Override
	public Object save(MetaData data) throws Exception {
		User u = (User) ServiceContext.getInstance().getUser();
		TRole role = us.userRole(u.getId());
		ContractorInfo contractor = (ContractorInfo) data;
		Object data1 = convert2Entity(data);
		if (1 == data.getOp()) {
			List<ORQuery> querys = new ArrayList<ORQuery>();
			querys.add(new ORQuery(Op.eq, "creditCode", contractor.getCreditCode()));
			List<TContractorInfo> contractors = dao.list(TContractorInfo.class, querys);
			if (contractors.size() > 0) {
				throw new Exception("社会信用代码重复,不能保存");
			} else {
				dao.save(data1);
			}
		} else {
			List<ORQuery> querys = new ArrayList<ORQuery>();
			//检测本企业是否存在同名承包商
			String companyCode = u.getCompanyCode();
			String contractorCode = contractor.getContractorCode();
			String contractorAlisa = contractor.getContractorAlisa();
			querys.add(new ORQuery(Op.eq, "companyCode", companyCode));
			querys.add(new ORQuery(Op.eq, "contractorAlisa", contractorAlisa));
			querys.add(new ORQuery(Op.neq, "contractorCode", contractorCode));
			List<TCompanyContractor> companyContractorList = dao.list(TCompanyContractor.class, querys);
			if (companyContractorList.size() > 0) {
				throw new Exception("该企业下存在同名承包商");
			}
			
			querys.clear();
			querys.add(new ORQuery(Op.eq, "creditCode", contractor.getCreditCode()));
			querys.add(new ORQuery(Op.neq, "contractorCode", contractor.getContractorCode()));
			List<TContractorInfo> contractors = dao.list(TContractorInfo.class, querys);
			if (contractors.size() > 0) {
				throw new Exception("社会信用代码重复,不能保存");
			} else {
				Object data2 = null;
				Serializable pk = (Serializable) orUtil.getEntityPk(data1);
				if (null != pk)
					data2 = dao.get(getEntityClass(), pk);
				if (null == data2)
					dao.save(data1);
				else {
					BeanUtils.copyProperties(data2, data1);
					dao.update(data2);
				}
				if ("2".equals(role.getRoleLevel())) {
					querys = new ArrayList<ORQuery>();
					querys.add(new ORQuery(Op.eq, "contractorCode", contractor.getContractorCode()));
					querys.add(new ORQuery(Op.eq, "companyCode", u.getCompanyCode()));
					List<TCompanyContractor> rels = dao.list(TCompanyContractor.class, querys);
					for (TCompanyContractor rel : rels) {
						rel.setContractorAlisa(contractor.getContractorAlisa());
						rel.setContractexpImg(contractor.getContractexpImg());
						rel.setContractImg(contractor.getContractImg());
						rel.setContractStampImg(contractor.getContractStampImg());
					}
				}
			}
		}
		ContractorInfo con2 = (ContractorInfo) convert2MetaData(data1);
		con2.setContractorAlisa(contractor.getContractorAlisa());
		return con2;
	}

	@Override
	public Object del(MetaData data) throws Exception {
		User u = (User) ServiceContext.getInstance().getUser();
		TRole role = us.userRole(u.getId());
		if ("9".equals(role.getRoleLevel())) {
			Object data1 = convert2Entity(data);
			data1 = dao.get(getEntityClass(), (Serializable) orUtil.getEntityPk(data1));
			if (null != data1)
				dao.delete(data1);
		} else if ("2".equals(role.getRoleLevel())) {
			ContractorInfo d = (ContractorInfo) data;
			List<ORQuery> querys = new ArrayList<ORQuery>();
			querys.add(new ORQuery(Op.eq, "contractorCode", d.getContractorCode()));
			querys.add(new ORQuery(Op.eq, "companyCode", u.getCompanyCode()));
			List<TCompanyContractor> rels = dao.list(TCompanyContractor.class, querys);
			for (TCompanyContractor rel : rels) {
				dao.delete(rel);
			}
		}
		return data;
	}
}
